package com.ruoyi.mf178.job;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.mf178.domain.Mf178Response;
import com.ruoyi.mf178.domain.SupplyGoodManageQuery;
import com.ruoyi.mf178.service.IMf178SupplyGoodService;
import com.ruoyi.mf178.service.Mf178Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class SupplyGoodSyncJob {
    @Value("${supplyGoodSyncJob}")
    private Boolean supplyGoodSyncJob;
    @Autowired
    Mf178Api mf178Api;
    @Autowired
    IMf178SupplyGoodService supplyGoodService;

    @Scheduled(cron = "0/60 * * * * ?")
    public void job() {
        if (!supplyGoodSyncJob) {
            return;
        }
        log.info("···开始同步商品定时任务");
        Integer page = 1;
        Integer size = 100;
        Integer total = 0;
        do {
            SupplyGoodManageQuery query = new SupplyGoodManageQuery();
            query.setB_id(18);
            query.setPage(page);
            query.setPageSize(size);
            Mf178Response resultJson = mf178Api.getSupplyGoodManageList(query);
            if (resultJson.getCode() != 0) {
                log.info("···查询失败，不同步信息");
                return;
            }
            JSONObject statInfo = JSONObject.parseObject(resultJson.getData().toString()).getJSONObject("stat_info");
            total = statInfo.getInteger("total");
            log.info("···第{}页查询成功，开始同步信息。", page);
            supplyGoodService.syncGoods(resultJson);
            page++;
        } while (page * size < total);

        log.info("···同步完成");
    }


}
